#include "task_cycle.h"
#include "hem_common.h"
#include "FreeRTOS.h"
#include "task.h"
#include "user_mb_app_s.h"
#include "app_input.h"
#include "app_output.h"
#include "task_modbus.h"

#define TASK_PRIO_STACKSIZE_500ms 128

static TaskHandle_t task_500ms_handle = NULL;

void Task_500ms(void);

void Task_500ms_init(void)
{
    xTaskCreate((TaskFunction_t)Task_500ms,"500ms_task",TASK_PRIO_STACKSIZE_500ms,NULL,TASK_PRIO_500ms,(TaskHandle_t *)task_500ms_handle); 
}
void in_parameters_setup(void)
{
      uint8_t index = 0;
      uint8_t mode = 0;
      for(index = 0; index < HEM_IN_PORT_NUM; index++)
      {
        mode = usSRegHoldBuf[64 + index];
        if((mode < 5) && (mode != g_config.in_mode[index]))
        {
            g_config.in_mode[index] = mode;
            if(index < 16)
            {
                //4~20mA,输入端口必须选择0~5V采样
                if(mode == 2) g_I0_to_I15_ai_config[index] = 1;
                else g_I0_to_I15_ai_config[index] = 0;
            }
            if((index >= 16) && (index < 18))
            {
                if(mode == 3) g_pca9555_port_func[index-15] = 0;
                else g_pca9555_port_func[index-15] = 1;
            }

            if((index >= 18) && (index < 26))
            {
                if(mode == 5) g_DI0_to_DI15_config[index-18] = 1;
                else g_DI0_to_DI15_config[index-18] = 0;
            }
            g_config.in_mode[index] = mode;
        }
      }
}

void pwm_parameters_setup(void)
{
    uint8_t index = 0;
    uint8_t change = 0;
	uint16_t freq = 0;
    for(index = 0;index < 6;index++)
    {
        freq = usSRegHoldBuf[120+index];
        if((g_config.pwm_freq[index] != freq) &&
           (freq > 20) && (freq < 60000)
          )
        {
            g_config.pwm_freq[index] = freq;
            change = 1;
        }
    }
    
    if(change == 1)
    {
        PWM_Init(&g_config.pwm_freq[0],6);
    }
}
void pwm_parameters_init(void)
{
    uint8_t index = 0;
    for(index = 0; index < 6; index++)
    {
        usSRegHoldBuf[120+index] = (uint16_t)(g_config.pwm_freq[index]);
    }
}
void can_parameters_setup(void)
{
    uint16_t new_baudrate = 0;
    uint16_t terminal_resistor = 0;
    uint8_t index = 0;
    for(index = 0;index < 2;index++)
    {
        new_baudrate = usSRegHoldBuf[130 + index];

        if ((new_baudrate != g_config.baudrate[index]) && 
            ((new_baudrate == 125) || (new_baudrate == 250) || (new_baudrate == 500) || (new_baudrate == 1000))
        )
        {
            g_config.baudrate[index] = new_baudrate;
        }

        terminal_resistor = usSRegHoldBuf[132 + index];
        if((terminal_resistor < 2) && (terminal_resistor != g_config.can_terminal_resistor[index]))
        {
            g_config.can_terminal_resistor[index] = terminal_resistor;
            if(index == 0)
            {
                g_pca9555_port_func[6] = g_config.can_terminal_resistor[index];
            }
            else if(index == 1)
            {
                g_pca9555_port_func[5] = g_config.can_terminal_resistor[index];
            }
        }
    } 
}
void can_parameters_init(void)
{
    uint8_t index = 0;
    for(index = 0; index < 2; index++)
    {
        usSRegHoldBuf[130 + index] = (uint16_t)(g_config.baudrate[index]);
        usSRegHoldBuf[132 + index] = (uint16_t)(g_config.can_terminal_resistor[index]);
    }
}
void pwmi_parameters_setup(void)
{
    uint16_t tPwmi_200ma = 0;
    uint16_t tPwmi_800ma = 0;
    uint8_t index = 0;
    uint8_t changed = 0;

    for(index = 0;index < HEM_PWMI_NUM;index++)
    {
        tPwmi_200ma = usSRegHoldBuf[140 + index];
        if((tPwmi_200ma > 120) && (tPwmi_200ma < 280) && (tPwmi_200ma != uc_pwmi_param_buf_200_mA[index]))
        {
            uc_pwmi_param_buf_200_mA[index] = tPwmi_200ma;
            changed = 1;
        }
        
        tPwmi_800ma = usSRegHoldBuf[152 + index];
        if((tPwmi_800ma > 600) && (tPwmi_800ma < 1000) && (tPwmi_800ma != uc_pwmi_param_buf_800_mA[index]))
        {
            uc_pwmi_param_buf_800_mA[index] = tPwmi_800ma;
            changed = 1;
        }
    }
    if(changed == 1)
    {
        g_pwmi_store_param_sn = 1;
    }
}
void pwmi_parameters_init(void)
{
    uint8_t index = 0;
    for(index = 0; index < HEM_PWMI_NUM; index++)
    {
        usSRegHoldBuf[140 + index] = (uint16_t)(uc_pwmi_param_buf_200_mA[index]);
        usSRegHoldBuf[152 + index] = (uint16_t)(uc_pwmi_param_buf_800_mA[index]);
    }
}
/**
 * modbus tcp初始化配置参数
 */
void mb_tcp_reg_init(void)
{
    in_parameters_init();
    pwm_parameters_init();
    can_parameters_init();
    pwmi_parameters_init();
}
void parameters_setup(void)
{
    in_parameters_setup();
    pwm_parameters_setup();
    can_parameters_setup();
    pwmi_parameters_setup();
}
void Task_500ms(void)
{
    mb_tcp_reg_init();
    while(1)
    {
        
        vTaskDelay(500);
    }
}

